package com.longshihan.news.model.http;

import com.longshihan.news.BuildConfig;
import com.longshihan.news.app.Constants;
import com.longshihan.news.model.bean.CommentBean;
import com.longshihan.news.model.bean.DailyBeforeListBean;
import com.longshihan.news.model.bean.DailyListBean;
import com.longshihan.news.model.bean.DetailExtraBean;
import com.longshihan.news.model.bean.GankItemBean;
import com.longshihan.news.model.bean.HotListBean;
import com.longshihan.news.model.bean.JokeBean;
import com.longshihan.news.model.bean.LishiBean;
import com.longshihan.news.model.bean.MsgStauts;
import com.longshihan.news.model.bean.RobotBean;
import com.longshihan.news.model.bean.SectionChildListBean;
import com.longshihan.news.model.bean.SectionListBean;
import com.longshihan.news.model.bean.ThemeChildListBean;
import com.longshihan.news.model.bean.ThemeListBean;
import com.longshihan.news.model.bean.TopNewsBean;
import com.longshihan.news.model.bean.WeixinBean;
import com.longshihan.news.model.bean.ZhihuDetailBean;
import com.longshihan.news.util.ZSystem;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Cache;
import okhttp3.CacheControl;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava.RxJavaCallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;
import rx.Observable;

/**
 * @创建者 longshihan
 * 聚合OKHTTP的网络请求类
 * @创建时间 2016/9/18 15:50
 */
public class RetrofitHelper {

    private static OkHttpClient sOkHttpClient = null;
    private static ZhihuApis zhihuApiService = null;
    private static GankApis sGankApis = null;
    private static JuHeApis sJuHeApis = null;
    private static JuHeApis sJuheApis = null;
    private static JuHeApis sJuheRApis = null;
    private static JuHeApis sJuHeLApis = null;
    private static LeXianApis sleXianApis = null;

    public RetrofitHelper() {
        init();
    }

    private void init() {
        initOkHttp();
        zhihuApiService = getZhihuApiService();
        sGankApis = getGankApisService();
        sJuHeApis = getJuHeApisService();
        sJuheApis = getJuHeJokeApisService();
        sJuheRApis = getJuheRobotApisService();
        sJuHeLApis = getJuheLishiApisService();
        sleXianApis = getLeXianApiService();
    }

    /**
     * 添加日志拦截，缓存，超时，错误重连
     */
    private void initOkHttp() {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (BuildConfig.DEBUG) {
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BASIC);
            builder.addInterceptor(loggingInterceptor);
        }
        File cacheFile = new File(Constants.PATH_CACHE);
        Cache cache = new Cache(cacheFile, 1024 * 1024 * 50);
        final Map<String, String> map = new HashMap<>();
        map.put("deviceType", "872dd68e4a0e0af7");
        map.put("appVersion", "3.7.0");
        map.put("deviceId", "3");
        map.put("brandName", "4");
        map.put("osVersion", "25");
        map.put("userId", "726");
        map.put("data", "iCRpcZ19F+lRC7ESGD192g==");
        map.put("mapPoint","0.0,0.0");
        map.put("brandName","MI 3/4");
        map.put("token",
                "xKTqW0cwfW9VWw7mz38SjhMGyU+/z" +
                        "+OI9m88GCfkr72eGE8O78tkxH3LlkPyJ5FbsBDpcViz4V35Gx7eVbvzaQ==");
        Interceptor cacheInterceptor = new Interceptor() {
            @Override
            public Response intercept(Chain chain) throws IOException {
                Request request = chain.request();
                Request.Builder requestBuilder = request.newBuilder();

                if (!ZSystem.isNetworkConnected()) {
                    requestBuilder.cacheControl(CacheControl.FORCE_CACHE).build();//无网络时只从缓存中读取
                }

               // request = injectParamsIntoUrl(request.url().newBuilder(), requestBuilder, map);

                Response response = chain.proceed(request);
                if (ZSystem.isNetworkConnected()) {
                    int maxAge = 0;
                    // 有网络时, 不缓存, 最大保存时长为0
                    response.newBuilder()
                            .header("Cache-Control", "public, max-age=" + maxAge)
                            .removeHeader("Pragma")
                            .build();
                } else {
                    // 无网络时，设置超时为4周
                    int maxStale = 60 * 60 * 24 * 28;
                    response.newBuilder()
                            .header("Cache-Control", "public, only-if-cached, max-stale=" +
                                    maxStale)
                            .removeHeader("Pragma")
                            .build();
                }
                return response;
            }
        };

        builder.cache(cache).addInterceptor(cacheInterceptor);
        //设置超时
        builder.connectTimeout(15, TimeUnit.SECONDS);
        builder.readTimeout(20, TimeUnit.SECONDS);
        builder.writeTimeout(20, TimeUnit.SECONDS);
        //错误重连
        builder.retryOnConnectionFailure(true);
        sOkHttpClient = builder.build();
    }


    private Request injectParamsIntoUrl(HttpUrl.Builder httpUrlBuilder, Request.Builder requestBuilder, Map<String, String> paramsMap) {
        if (paramsMap.size() > 0) {
            Iterator iterator = paramsMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry entry = (Map.Entry) iterator.next();
                httpUrlBuilder.addQueryParameter((String) entry.getKey(), (String) entry.getValue());
            }
            requestBuilder.url(httpUrlBuilder.build());
            return requestBuilder.build();
        }

        return null;
    }


    //知乎数据获取
    private static ZhihuApis getZhihuApiService() {
        Retrofit zhihuRetrofit = new Retrofit.Builder()
                .baseUrl(ZhihuApis.HOST)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return zhihuRetrofit.create(ZhihuApis.class);
    }

    public Observable<DailyListBean> fetchDailyListInfo() {
        return zhihuApiService.getDailyList();
    }

    public Observable<DailyBeforeListBean> fetchDailyBeforeListInfo(String date) {
        return zhihuApiService.getDailyBeforeList(date);
    }

    public Observable<ThemeListBean> fetchThemeListBean() {
        return zhihuApiService.getThemeList();
    }

    public Observable<DetailExtraBean> fetchDetailExtraBean(int id) {
        return zhihuApiService.getDetailExtraInfo(id);
    }

    public Observable<ZhihuDetailBean> fetchDetailInfo(int id) {
        return zhihuApiService.getDetailInfo(id);
    }

    //    public Observable<WelcomeBean> fetchWelcomeBean() {
    //        return sGankApis.getFuliList(1);
    //    }

    public Observable<CommentBean> fetchLongCommentBean(int id) {
        return zhihuApiService.getLongCommentInfo(id);
    }

    public Observable<CommentBean> fetchShortCommentBean(int id) {
        return zhihuApiService.getShortCommentInfo(id);
    }

    public Observable<HotListBean> fetchHotList() {
        return zhihuApiService.getHotList();
    }

    public Observable<SectionListBean> fetchSectionList() {
        return zhihuApiService.getSectionList();
    }

    public Observable<SectionChildListBean> fetchSectionChildList(int id) {
        return zhihuApiService.getSectionChildList(id);
    }

    public Observable<ThemeChildListBean> fetchThemeChildList(int id) {
        return zhihuApiService.getThemeChildList(id);
    }

    //gank数据获取
    public static GankApis getGankApisService() {
        Retrofit gankRetrofit = new Retrofit.Builder()
                .baseUrl(GankApis.HOST)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return gankRetrofit.create(GankApis.class);
    }

    public Observable<HttpResponse<List<GankItemBean>>> fetchFuliImage(int num) {
        return sGankApis.getFuliList(num);
    }

    public Observable<HttpResponse<List<GankItemBean>>> fetchGirlList(int num, int page) {
        return sGankApis.getGirlList(num, page);
    }

    public Observable<HttpResponse<List<GankItemBean>>> fetchTechList(String tech, int num, int
            page) {
        return sGankApis.getTechList(tech, num, page);
    }

    //集合数据获取
    public static JuHeApis getJuHeApisService() {
        Retrofit juheRetrofit = new Retrofit.Builder()
                .baseUrl(JuHeApis.HOST)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return juheRetrofit.create(JuHeApis.class);
    }

    public Observable<HttpResponse<TopNewsBean.ResultBean>> fetchTopNewsList() {
        return sJuHeApis.getTopNewsList();
    }

    public Observable<HttpResponse<WeixinBean.ResultBean>> fetchWeixinList(int ps, int pno) {
        return sJuHeApis.getWeixinList(Constants.JUHE_WEIXIN_KEY, ps, pno);
    }

    //聚合 数据
    public static JuHeApis getJuHeJokeApisService() {
        Retrofit juheRetrofit = new Retrofit.Builder()
                .baseUrl(JuHeApis.HOST_JOKE)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return juheRetrofit.create(JuHeApis.class);
    }

    //getJokeList(@Query("key")String key,@Query("page")int page,
    //@Query("pagesize") int pagesize, @Query("sort") String sort, @Query("time") String time);
    public Observable<HttpResponse<JokeBean.ResultBean>> fetchJokeBean(int page, int pagesize,
                                                                       String sort, String time) {
        return sJuheApis.getJokeList(Constants.JUHE_JOKE_KEY, page, pagesize, sort, time);
    }

    //机器人聚合 数据
    public static JuHeApis getJuheRobotApisService() {
        Retrofit juheRetrofit = new Retrofit.Builder()
                .baseUrl(JuHeApis.HOST_ROBOT)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return juheRetrofit.create(JuHeApis.class);
    }

    public Observable<HttpResponse<RobotBean.ResultBean>> fetchRobotAnswer(String info) {
        return sJuheRApis.getRobotInfo(info, Constants.JUHE_ROBOT_KEY);
    }

    //历史上的今天 数据
    public static JuHeApis getJuheLishiApisService() {
        Retrofit juheRetrofit = new Retrofit.Builder()
                .baseUrl(JuHeApis.HOST_LISHI)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return juheRetrofit.create(JuHeApis.class);
    }

    public Observable<HttpResponse<List<LishiBean.ResultBean>>> fetchLishiInfo(int month, int day) {
        return sJuHeLApis.getLishiInfo(Constants.JUHE_LISHI_KEY, "1.0", month, day);
    }

    public Observable<MsgStauts> fetchInit() {
        return sleXianApis.getStatus();
    }


    public LeXianApis getLeXianApiService() {
        Retrofit lexianRetrofit = new Retrofit.Builder()
                .baseUrl(LeXianApis.HOST1)
                .client(sOkHttpClient)
                .addConverterFactory(GsonConverterFactory.create())
                .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
                .build();
        return lexianRetrofit.create(LeXianApis.class);
    }
}
